package com.shundr.common;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.shundr.base.BaseListViewFragment;
import com.shundr.cargo.http.CargoRequest;
import com.shundr.common.util.ProgressDialogHelper;
import com.shundr.common.util.ToastHelper;
import com.shundr.common.view.CListView.OnLoadMoreListener;
import com.shundr.common.view.CListView.OnRefreshListener;
import com.shundr.frame.constant.LContext;
import com.shundr.frame.http.LHttpRequest;
import com.shundr.frame.util.LLogger;
import com.shundr.order.OrderDetailsActivity;
import com.shundr.order.adapter.OrderItemAdapter;
import com.shundr.order.http.OrderRequest;
import com.shundr.order.model.CargoOrderInfo;
import com.shundr.user.LoginActivity;
import com.shundr.user.util.UserListener;
import com.shundr.user.util.UserUtil;

/**
 * 订单列表
 * OrderFragment
 * 
 * @author larry 2015-4-29 上午9:54:29 
 * @version 1.0.0
 *
 */
public class OrderFragment extends BaseListViewFragment implements UserListener{

	private static final int PAGE_SIZE = LContext.PAGE_SIZE;
	private int pageNo = 0;
	
	private String mStatus = CargoOrderInfo.OrderStatus.ALL + "";
	
	private OrderItemAdapter mAdapter;
	
	private List<CargoOrderInfo> mList = new ArrayList<CargoOrderInfo>();
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case CargoRequest.GET_CARGO_LIST_SUCCESSED:
					List<CargoOrderInfo> infos = (List<CargoOrderInfo>)msg.obj;
					if (infos != null) {
						if (infos.size() > 0) {
							if (pageNo == 0) {
								mList.clear();
								mListView.setVisibility(View.VISIBLE);
								mLayoutNoContent.setVisibility(View.GONE);
								if (infos.size() >= PAGE_SIZE) {
									mListView.setCanLoadMore(true);
									mListView.setAutoLoadMore(true);
									mListView.setCanRefresh(true);
								} else {
									mListView.setCanLoadMore(false);
									mListView.setAutoLoadMore(false);
									mListView.setCanRefresh(false);
								}
							}
							mList.addAll(infos);
							mAdapter.notifyDataSetChanged();
							if (pageNo == 0) {
								mListView.setSelection(0);
							}
							pageNo = pageNo + 1;
						} else {
							if (pageNo == 0) {
								mListView.setVisibility(View.GONE);
								mLayoutNoContent.setVisibility(View.VISIBLE);
							} else {
								ToastHelper.toastLong(getActivity(), "最后一页");
							}
						}
					}
					ProgressDialogHelper.dismiss();
					mListView.onRefreshComplete();
					mListView.onLoadMoreComplete();
					break;
				case LHttpRequest.REQUEST_FAILED:
					ProgressDialogHelper.dismiss();
					mListView.onLoadMoreComplete();
					mListView.onRefreshComplete();
					ToastHelper.toastShort(getActivity(), msg.obj.toString());
					break;

				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}
		
	};
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		LLogger.e("onActivityCreated");
		mAdapter = new OrderItemAdapter(mList, mContext);
		mListView.setAdapter(mAdapter);
		mListView.setOnRefreshListener(new OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				pageNo = 0;
				new OrderRequest(mContext, mHandler).getOrderLsit(PAGE_SIZE, mStatus, pageNo * PAGE_SIZE);
			}
		});
		mListView.setOnLoadListener(new OnLoadMoreListener() {
			@Override
			public void onLoadMore() {
				new OrderRequest(mContext, mHandler).getOrderLsit(PAGE_SIZE, mStatus, pageNo * PAGE_SIZE);
			}
		});
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try {
					String orderId = mList.get(position-1).getOrderId()+"";
					Intent intent=new Intent(getActivity(), OrderDetailsActivity.class);
					intent.putExtra("orderId", orderId);
					startActivity(intent);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
//		onLogined();
	}

	public void onLogined(){
		if(LContext.isLogin()){
			mLayoutNoContent.setVisibility(View.GONE);
			mListView.setVisibility(View.VISIBLE);
			pageNo = 0;
			new OrderRequest(mContext, mHandler).getOrderLsit(PAGE_SIZE, mStatus, pageNo * PAGE_SIZE);
		}else{
			mLayoutNoContent.setVisibility(View.VISIBLE);
			mListView.setVisibility(View.GONE);
			tvNoContent.setText("您还未登录,点我登录");
			tvNoContent.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					startActivity(new Intent(getActivity(), LoginActivity.class));
				}
			});
		}
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		UserUtil.mUserListeners.remove(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		onLogined();
		UserUtil.mUserListeners.add(this);
	}
	
	public void onRefesh(){
//		if(mStatus == status)
//			return;
		ProgressDialogHelper.show(mContext, "刷新中");
		pageNo = 0;
		new OrderRequest(mContext, mHandler).getOrderLsit(PAGE_SIZE, mStatus, pageNo * PAGE_SIZE);
	}

	public void onChanged(String status){
//		if(mStatus == status)
//			return;
		mStatus = status;
		pageNo = 0;
		new OrderRequest(mContext, mHandler).getOrderLsit(PAGE_SIZE, mStatus, pageNo * PAGE_SIZE);
	}

	@Override
	public void onUserChanged(int flag) {
		LLogger.e("larry","登录 ：" + flag);
		if(flag == 0){//登录
			mLayoutNoContent.setVisibility(View.GONE);
			tvNoContent.setText("没有数据");
			tvNoContent.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
				}
			});
			mListView.setVisibility(View.VISIBLE);
			pageNo = 0;
			new OrderRequest(mContext, mHandler).getOrderLsit(PAGE_SIZE, mStatus, pageNo * PAGE_SIZE);
		}else if(flag == 1){//注销
			mLayoutNoContent.setVisibility(View.VISIBLE);
			mListView.setVisibility(View.GONE);
			tvNoContent.setText("您还未登录,点我登录");
			tvNoContent.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					startActivity(new Intent(getActivity(), LoginActivity.class));
				}
			});
		}	
	}
	
}
